<script setup>
import { ref, onMounted } from 'vue'

const mobileMenuOpen = ref(false)
const currentUser = ref(null)

const navItems = [
  { label: '首页', href: '/' },
  { label: '任务管理', href: '/tasks' },
  { label: '应用卡券', href: '#' },
  { label: '为何选择', href: '#' },
  { label: '1分钟内', href: '#' },
  { label: '快捷指南', href: '#' },
  { label: '社区', href: '#' }
]

// 检查登录状态
onMounted(() => {
  const userStr = localStorage.getItem('currentUser')
  if (userStr) {
    try {
      currentUser.value = JSON.parse(userStr)
    } catch (error) {
      console.error('解析用户信息失败:', error)
    }
  }
})

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('currentUser')
  currentUser.value = null
  window.location.href = '/login'
}
</script>

<template>
  <header class="header">
    <div class="top-banner">
      <p>🎁 OY不注册账户，更传递数字资产的保护和监管</p>
    </div>
    
    <nav class="navbar">
      <div class="container">
        <div class="nav-content">
          <a href="/" class="logo">
            OY<span class="logo-highlight">交易所下载</span>
          </a>
          
          <div class="nav-links" :class="{ 'mobile-open': mobileMenuOpen }">
            <a 
              v-for="item in navItems" 
              :key="item.label"
              :href="item.href"
              class="nav-link"
            >
              {{ item.label }}
            </a>
          </div>
          
          <div class="nav-actions">
            <button class="lang-switch">简体中文</button>
            <button class="lang-switch">English</button>
            
            <!-- 用户登录状态 -->
            <div v-if="currentUser" class="user-info">
              <span class="username">{{ currentUser.username }}</span>
              <button class="btn-logout" @click="handleLogout">退出</button>
            </div>
            <a v-else href="/login" class="btn-login">登录/注册</a>
          </div>
          
          <button 
            class="mobile-menu-btn"
            @click="mobileMenuOpen = !mobileMenuOpen"
          >
            ☰
          </button>
        </div>
      </div>
    </nav>
  </header>
</template>

<style scoped>
.header {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.top-banner {
  background: linear-gradient(90deg, #1a1d29 0%, #2d3142 100%);
  color: white;
  padding: 8px 0;
  text-align: center;
  font-size: 14px;
}

.navbar {
  background: white;
  padding: 16px 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
}

.nav-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 32px;
}

.logo {
  font-size: 24px;
  font-weight: 700;
  color: #00d4aa;
  white-space: nowrap;
}

.logo-highlight {
  color: #1a1d29;
}

.nav-links {
  display: flex;
  gap: 32px;
  flex: 1;
  justify-content: center;
}

.nav-link {
  color: #333;
  font-size: 15px;
  font-weight: 500;
  transition: color 0.3s;
  white-space: nowrap;
}

.nav-link:hover {
  color: #00d4aa;
}

.nav-actions {
  display: flex;
  gap: 12px;
}

.lang-switch {
  background: transparent;
  color: #666;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  transition: all 0.3s;
}

.lang-switch:hover {
  background: #f5f7fa;
  color: #00d4aa;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: #f5f7fa;
  border-radius: 6px;
}

.username {
  color: #333;
  font-size: 14px;
  font-weight: 500;
}

.btn-logout {
  background: #f56c6c;
  color: white;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 13px;
  transition: all 0.3s;
}

.btn-logout:hover {
  background: #f45454;
}

.btn-login {
  background: #00d4aa;
  color: white;
  padding: 8px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.btn-login:hover {
  background: #00b894;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 212, 170, 0.3);
}

.mobile-menu-btn {
  display: none;
  background: transparent;
  font-size: 24px;
  padding: 8px;
}

@media (max-width: 768px) {
  .nav-links {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    flex-direction: column;
    padding: 16px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  }
  
  .nav-links.mobile-open {
    display: flex;
  }
  
  .nav-actions {
    display: none;
  }
  
  .mobile-menu-btn {
    display: block;
  }
}
</style>
